package com.myfoon.action.backing.system;

import com.myfoon.model.ejb.EventEJBLocal;
import com.myfoon.model.ejb.UserEJBLocal;
import com.myfoon.model.entity.CountryName;
import com.myfoon.model.entity.Event;
import com.myfoon.model.entity.User;
import com.myfoon.model.entity.result.EventOfFriend;
import com.myfoon.view.application.MyConstants;
import com.myfoon.view.context.helper.EJBReceiver;

import java.util.List;

import javax.ejb.EJBException;

import javax.persistence.EntityExistsException;

public class EventBean {
    public EventBean() {
    }
    
    public Event persist(Event event) {
        EJBReceiver receiver = new EJBReceiver();
        
        EventEJBLocal local =
            (EventEJBLocal)receiver.receiveEJB(MyConstants.jndiName + "EventEJB/local");
        
        try {
            event = local.persist(event);
        }
        catch(EJBException e) {
            if (e.getCausedByException() instanceof EntityExistsException) {                
                throw new EntityExistsException();
            }
            e.printStackTrace();
        }
        
        return event;    
    }    
    
    public List<EventOfFriend> getLastEvents(User user, Integer timezone, boolean friendsEvents, int start, int max) {
        EJBReceiver receiver = new EJBReceiver();
        
        EventEJBLocal local =
            (EventEJBLocal)receiver.receiveEJB(MyConstants.jndiName + "EventEJB/local");
                
        return local.getLastEvents(user, timezone, friendsEvents, start, max);
    }        
}
